package com.question.business.menu.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.question.business.menu.entity.MenuInterface;
import com.question.business.menu.mapper.MenuInterfaceMapper;
import com.question.business.menu.model.ao.MenuInterfaceAO;
import com.question.business.menu.model.vo.MenuInterfaceVO;
import com.question.business.menu.service.IMenuInterfaceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 菜单接口服务实现类
 *
 * @author star
 * @since 2023-09-11
 */
@Slf4j
@Service
public class MenuInterfaceServiceImpl extends ServiceImpl<MenuInterfaceMapper, MenuInterface> implements IMenuInterfaceService {

    /**
     * 菜单接口查询
     *
     * @param menuId 菜单ID
     * @return List<MenuInterfaceVO> 响应参数
     */
    @Override
    public List<MenuInterfaceVO> queryMenuInterfaceList(String menuId) {
        return baseMapper.queryMenuInterfaceList(menuId);
    }

    /**
     * 菜单接口新增数据
     *
     * @param menuId     菜单ID
     * @param interfaces 菜单接口集合
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addMenuInterface(String menuId, List<MenuInterfaceAO> interfaces) {
        // 删除历史菜单接口
        baseMapper.deleteByMenuId(menuId);
        if (CollectionUtils.isNotEmpty(interfaces)) {
            List<MenuInterface> data = new ArrayList<>(interfaces.size());
            for (MenuInterfaceAO ao : interfaces) {
                MenuInterface entity = new MenuInterface();
                entity.setMenuId(menuId);
                entity.setUrl(ao.getUrl());
                data.add(entity);
            }
            super.saveBatch(data);
        }
    }

}